import {createSSRApp} from 'vue'
import App from './App.vue'
import {createPinia} from 'pinia'

// 创建应用实例
const app = createSSRApp(App)

// 使用 Pinia
const pinia = createPinia()
app.use(pinia)



const checkAuth = () => {
    const token = uni.getStorageSync('token')
    return !!token
}



// 注册全局前置守卫
uni.addInterceptor('navigateTo', {
    invoke(args) {
        // 需要登录的页面路径
        const authPages = [
            '/pages/profile/index'
            , '/pages/profile/edit'
            , '/pages/profile/password'
        ]

        if (authPages.includes(args.url.split('?')[0]) && !checkAuth()) {
            uni.showToast({
                title: '请先登录',
                icon: 'none'
            })
            uni.navigateTo({
                url: '/pages/auth/login'
            })
            return false
        }
        return true
    }
})

// 挂载应用
export function createApp() {
    return {
        app
    }
}
